﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MacViet.DataBaseAPI;

public partial class Control_Manager_Services_and_Product_USServices : System.Web.UI.UserControl
{
    SERVICE_CATEGROIESDataAPI servicescat = new SERVICE_CATEGROIESDataAPI();
    SERVICESDataAPI services = new SERVICESDataAPI();
    protected void Page_Load(object sender, EventArgs e)
    {
          if (!IsPostBack)
        {
            Multiview2.ActiveViewIndex = 0;
            fillServicesTypeInGrid3();
            fillServicesInGrid4();
        }
       
    }
 

    protected void btnServices_Click(object sender, EventArgs e)
    {


        Multiview2.ActiveViewIndex = 0;
    }
    protected void btnServicesCat_Click(object sender, EventArgs e)
    {
        Multiview2.ActiveViewIndex = 1;
    }
    protected void fillServicesTypeInGrid3()
    {
        int accId = Convert.ToInt32(Session["ACC_ID"]);
        DataTable dt = servicescat.getByACC_ID(accId);
        GrvServices_Cat.DataSource = dt;
        GrvServices_Cat.DataBind();

    }
    public int getAcc()
    {
        int accId = Convert.ToInt32(Session["ACC_ID"]);
            return accId;
    }
    protected void GrvServices_Cat_RowEditing(object sender, GridViewEditEventArgs e)
    {

        GrvServices_Cat.EditIndex = e.NewEditIndex;
        fillServicesTypeInGrid3();

    }
    protected void GrvServices_Cat_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GrvServices_Cat.EditIndex = -1;
        fillServicesTypeInGrid3();
    }
    protected void GrvServices_Cat_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
            TextBox txtName = (TextBox)GrvServices_Cat.Rows[e.RowIndex].FindControl("txtSercat_Name");
            int Services_Cat_ID = Int32.Parse(GrvServices_Cat.DataKeys[e.RowIndex].Values[0].ToString());
            servicescat.updateSERVICE_CATEGROIES(Services_Cat_ID, txtName.Text);
            GrvServices_Cat.EditIndex = -1;
            fillServicesTypeInGrid3();
        }
        catch(Exception ex)
        {
            Response.Write("<script>alert('Error input Data,you can not Update Services_Cat')</script>");
        }

    }
    protected void btnAddSerCat_Click(object sender, EventArgs e)
    {
        try
        {
            int acc = getAcc();
                TextBox txtName = (TextBox)GrvServices_Cat.FooterRow.FindControl("txtNewSercat_Name");
                servicescat.insertSERVICE_CATEGROIES(acc, txtName.Text);
                fillServicesTypeInGrid3();
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('Error input Data,you can not AddProduct')</script>");
        }
    }
    protected void GrvServices_RowDataBound2(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList cmbServices_cat = (DropDownList)e.Row.FindControl("ServicesCatagory_ID");
            DataTable dt = servicescat.getAll();
            if (cmbServices_cat != null)
            {
                cmbServices_cat.DataSource = dt;
                cmbServices_cat.DataBind();
                cmbServices_cat.SelectedValue = GrvServices.DataKeys[0].Value.ToString();
            }
        }
    }
    protected void GrvServices_Cat_RowCommand1(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName.Equals("AddNewServices_Cat"))
            {
                int acc = getAcc();
                TextBox txtName = (TextBox)GrvServices_Cat.FooterRow.FindControl("txtNewSercat_Name");
                servicescat.insertSERVICE_CATEGROIES(acc, txtName.Text);
                fillServicesTypeInGrid3();
            }
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('Error input Data,you can not AddProduct')</script>");
        }
    }
    //protected void GrvServices_Cat_RowCommand(object sender, GridViewCommandEventArgs e)
    //{
    //    if (e.CommandName.Equals("Add New"))
    //    {
    //        TextBox txtName = (TextBox)GrvServices_Cat.FooterRow.FindControl("txtNewSercat_Name");
    //        servicescat.insertSERVICE_CATEGROIES(txtName.Text);
    //        fillServicesTypeInGrid3();
    //    }
    //}  


   //Xu ly Gridview4 Services
    protected void fillServicesInGrid4()
    {
        int acc = getAcc();
        DataTable dt = services.getServices_Edit(acc);
        GrvServices.DataSource = dt;
        GrvServices.DataBind();

    }
   
    protected void GrvServices_RowEditing(object sender, GridViewEditEventArgs e)
    {

        GrvServices.EditIndex = e.NewEditIndex;
        fillServicesInGrid4();

    }
    protected void GrvServices_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GrvServices.EditIndex = -1;
        fillServicesInGrid4();
    }
    protected void btnDeleteSer_Click(object sender, EventArgs e)
    {
        try
        {
            for (int i = 0; i < GrvServices.Rows.Count; i++)
            {
                string id = string.Empty;
                GridViewRow rows = GrvServices.Rows[i];
                bool chekBox = (rows.FindControl("chkserSelect") as CheckBox).Checked;
                if (chekBox)
                {
                    if (id == string.Empty)
                    {
                        id = GrvServices.DataKeys[i].Value.ToString();
                        services.deleteByPrimaryKeys(int.Parse(id));
                        Response.Write("<script>alert('Delete Sevices Success!')</script>");
                        fillServicesInGrid4();
                    }
                    else
                        id += GrvServices.DataKeys[i].Value.ToString() + ",";
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('Error input Data,you can not DeletedProduct')</script>");
        }
    }
    private DataTable LoadServiceType()
    {
        int acc = getAcc();
        DataTable dt = servicescat.getByACC_ID(acc);
        if (dt != null)
            return dt;
        return null;
    }
  

    //protected void GrvServices_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    //{
    //    //if (e.Row.RowType == DataControlRowType.DataRow)
    //    //{
    //    //    DropDownList cmbServices = e.Row.FindControl("cmbSerCat_ID") as DropDownList;
    //    //    cmbServices.DataSource = LoadServiceType();
    //    //    cmbServices.DataTextField = "SERCAT_NAME";
    //    //    cmbServices.DataValueField = "SERCAT_ID";
    //    //    cmbServices.DataBind();
    //    //}
    //}
    protected void GrvServices_RowDataBound1(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList cmbServices = (DropDownList)e.Row.FindControl("cmbSerCat_ID");

            if (cmbServices != null)
            {
                cmbServices.DataSource = LoadServiceType();

                cmbServices.DataBind();
                cmbServices.SelectedValue = GrvServices.DataKeys[0].Value.ToString();
            }
        }

        if (e.Row.RowType == DataControlRowType.Footer)
        {
            DropDownList cmbNewServices = (DropDownList)e.Row.FindControl("cmbNewSerCat_ID");
            cmbNewServices.DataSource = LoadServiceType();
            cmbNewServices.DataBind();
        } 
    }


    protected void GrvServices_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
            DropDownList cmbSevicesCat = GrvServices.Rows[e.RowIndex].FindControl("cmbSerCat_ID") as DropDownList;
            TextBox txtName = (TextBox)GrvServices.Rows[e.RowIndex].FindControl("txtServices_Name");
            TextBox txtDescription = (TextBox)GrvServices.Rows[e.RowIndex].FindControl("txtServices_Des");
            TextBox txtPrice = (TextBox)GrvServices.Rows[e.RowIndex].FindControl("txtProduct_Services");
            int value = Int32.Parse(cmbSevicesCat.SelectedValue.ToString());
            int serID = Int32.Parse(GrvServices.DataKeys[e.RowIndex].Values[0].ToString());
            decimal prices = Decimal.Parse(txtPrice.Text);
            services.updateSERVICES(serID, value, txtName.Text, txtDescription.Text, prices);
            GrvServices.EditIndex = -1;
            fillServicesInGrid4();
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('Error input Data,you can not Update Services')</script>");
        }
    }

    protected void GrvServices_RowCommand1(object sender, GridViewCommandEventArgs e)
    {
        //try
        //{
        //    if (e.CommandName.Equals("AddNewServices"))
        //    {
        //        DropDownList cmbNewSerCat = (DropDownList)GrvServices.FooterRow.FindControl("cmbNewSerCat_ID");
        //        TextBox txtNewName = (TextBox)GrvServices.FooterRow.FindControl("txtNewServices_Name");
        //        TextBox txtDes = (TextBox)GrvServices.FooterRow.FindControl("txtNewServices_Des");
        //        TextBox txtPrices = (TextBox)GrvServices.FooterRow.FindControl("txtNewServices_Price");
        //        //int serID = Int32.Parse(GrvServices.DataKeys[e.RowIndex].Values[0].ToString());
        //        decimal prices = Decimal.Parse(txtPrices.Text);
        //        int value = Int32.Parse(cmbNewSerCat.SelectedValue.ToString());
        //        services.insertSERVICES(value, txtNewName.Text, txtDes.Text, prices);
        //        fillServicesInGrid4();
        //    }
        //}
        //catch(Exception ex)
        //{
        //    Response.Write("<script>alert('Error input Data,you can not Add Services')</script>");
        //}
    }
    protected void btnAddServices_Click(object sender, EventArgs e)
    {
        try
        {
            
                DropDownList cmbNewSerCat = (DropDownList)GrvServices.FooterRow.FindControl("cmbNewSerCat_ID");
                TextBox txtNewName = (TextBox)GrvServices.FooterRow.FindControl("txtNewServices_Name");
                TextBox txtDes = (TextBox)GrvServices.FooterRow.FindControl("txtNewServices_Des");
                TextBox txtPrices = (TextBox)GrvServices.FooterRow.FindControl("txtNewServices_Price");
                //int serID = Int32.Parse(GrvServices.DataKeys[e.RowIndex].Values[0].ToString());
                decimal prices = Decimal.Parse(txtPrices.Text);
                int value = Int32.Parse(cmbNewSerCat.SelectedValue.ToString());
                services.insertSERVICES(value, txtNewName.Text, txtDes.Text, prices);
                fillServicesInGrid4();
            
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('Error input Data,you can not Add Services')</script>");
        }
    }

    
}
